package org.opens.javaskill.system;

import com.sun.security.auth.module.NTSystem;
import org.junit.Test;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class GetSystemEnv {

    /**
     * 功能:
     *      尝试运行它, 就会得到你系统的环境变量.
     *      -注意
     *          这个方法中有Map的lambda语法遍历Map
     */
    @Test
    public void test1() {
        System.getenv().forEach((key, value) -> System.out.println(key + ": " + value));
    }

    @Test
    public void getUserHome() {
        String home = System.getProperty("POD_NAME");
        System.out.println(home);
    }

    @Test
    public void getEnv() {
        String podName = System.getenv("POD_NAME");
        System.out.println(podName);
    }

    @Test
    public void test66() {
        NTSystem ntSystem = new NTSystem();
        System.out.println(ntSystem.getDomain());
        System.out.println(ntSystem.getName());
        System.out.println(ntSystem.getGroupIDs());
        System.out.println(ntSystem.getImpersonationToken());
        System.out.println(ntSystem.getPrimaryGroupID());
        System.out.println(ntSystem.getUserSID());
    }

    @Test
    public void testGetNetworkInterfaces() {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                System.out.println("名称: " + networkInterface.getName());
                System.out.println("显示名称: " + networkInterface.getDisplayName());
                System.out.println("是否虚拟网卡: " + networkInterface.isVirtual());
                byte[] hardwareAddress = networkInterface.getHardwareAddress();
                if (hardwareAddress != null) {
                    StringBuilder builder = new StringBuilder();
                    for (byte item : hardwareAddress) {
                        builder.append(String.format("%02X", item));
                        builder.append(":");
                    }
                    if (builder.length() > 0) {
                        builder.deleteCharAt(builder.length() - 1);
                    }
                    System.out.println("硬件地址: " + builder.toString());
                }

                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    if (!(inetAddress instanceof Inet4Address)) {
                        continue;
                    }
                    System.out.println("\t地址: " + inetAddress.getHostAddress());
                    System.out.println("\t判断1: " + inetAddress.isLoopbackAddress());
                    System.out.println("\t判断2: " + inetAddress.isLinkLocalAddress());
                }

                System.out.println();
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

}
